package edu.uic.kott.activities;

import edu.uic.keyoutcomestrackertool.R;
import edu.uic.kott.db.KottDataSource;
import edu.uic.kott.model.Instructor;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RadioButton;
public class AddEditInstructor extends Activity {

	private RadioButton instructorRadioButton;
	private RadioButton taRadioButton;
	private EditText nameEditText;
	private EditText officeLocationEditText;
	private EditText emailEditText;
	private EditText phoneEditText;

	private String id;
	private String type;
	private String name;
	private String officeLocation;
	private String email;
	private String phone;

	KottDataSource datasource;
	private static final String LOGTAG = "KOTT";
	private String option;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_edit_instructor);

		datasource = new KottDataSource(this);

		nameEditText = (EditText) findViewById(R.id.instructorTaNameEditText);
		officeLocationEditText = (EditText) findViewById(R.id.officeLocationEditText);
		emailEditText = (EditText) findViewById(R.id.emailEditText);
		phoneEditText = (EditText) findViewById(R.id.phoneNoEditText);

		instructorRadioButton = (RadioButton) findViewById(R.id.instructorRadio);
		taRadioButton = (RadioButton) findViewById(R.id.taRadio);

		//by default instructor is selected
		id="0";
		type = "Instructor";
		name = "";
		officeLocation = "Not Available";
		email = "Not Available";
		phone = "Not Available";

		option = "add";		//by default it is add new instructor

		Bundle extras = getIntent().getExtras();

		if (extras != null){

			option = extras.getString("option");

			if("update".equalsIgnoreCase(option)){
				Log.i(LOGTAG,"Inside AddEdit for updating instructor");

				option = "update";

				Log.i(LOGTAG,"Option changed to " + option);

				id = extras.getString("id");
				type = extras.getString("type");
				name = extras.getString("name");
				officeLocation = extras.getString("officeLocation");
				email = extras.getString("email");
				phone = extras.getString("phone");

				Log.i(LOGTAG,"Instructor id is:" + id);
				Log.i(LOGTAG,"type is :" + type);
				Log.i(LOGTAG,"name is :" + name);
				Log.i(LOGTAG,"phoneNo is :" + phone);
				Log.i(LOGTAG,"emailAdd is :" + email);
				Log.i(LOGTAG,"officeLocation is :" + officeLocation);

				nameEditText.setText(name);
				if("Teaching Assistant".equalsIgnoreCase(type))
					taRadioButton.setChecked(true);

				if(!email.equalsIgnoreCase("Not Available"))
					emailEditText.setText(email);

				if(!phone.equalsIgnoreCase("Not Available"))
					phoneEditText.setText(phone);

				if(!officeLocation.equalsIgnoreCase("Not Available"))
					officeLocationEditText.setText(officeLocation);
			}
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_save, menu);		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (taRadioButton.isChecked())
			type = "Teaching Assistant";
		else if (instructorRadioButton.isChecked())
			type = "Instructor";

		if(nameEditText.getText().toString().length() > 0){
			name = nameEditText.getText().toString();

			//get office location
			if (officeLocationEditText.getText().toString().length() > 0)
				officeLocation = officeLocationEditText.getText().toString();

			//get email id
			if (emailEditText.getText().toString().length() > 0)
				email = emailEditText.getText().toString();

			//get phone number
			if (phoneEditText.getText().toString().length() > 0)
				phone = phoneEditText.getText().toString();

			//Intent intent = new Intent(this, Instructors.class);

			Log.i(LOGTAG,"Save button clicked for " + option);

			Instructor instructor = new Instructor();
			instructor.setInstructorType(type);
			instructor.setInstructorName(name);
			instructor.setEmailAdd(email);
			instructor.setPhoneNo(phone);
			instructor.setOfficeLocation(officeLocation);

			if("update".equalsIgnoreCase(option)){
				Log.i(LOGTAG,"Call instructorUpdate()");
				Log.i(LOGTAG,"Instructor to be updated is " + String.valueOf(id));

				instructor.setInstructorId(Long.parseLong(id));
				datasource.updateInstructor(instructor);
			}

			//if adding new instructor via instructor list or while adding a course
			if("add".equalsIgnoreCase(option) || "back to AddEditCourse".equalsIgnoreCase(option)){
				Log.i(LOGTAG,"Call createInstructor()");
				long instructorId = datasource.createInstructor(instructor);
				Log.i(LOGTAG,"Instructor " + instructor.getInstructorName() + " created with id " + instructorId);
			}			

			if("back to AddEditCourse".equalsIgnoreCase(option)){
				Intent intent = new Intent(this, AddEditCourse.class);
				setResult(RESULT_OK, intent);
			}
			//else startActivity(new Intent(this, Instructors.class));

			finish();
		}

		//if name is not entered display error dialog
		else{

			AlertDialog.Builder builder = new AlertDialog.Builder(AddEditInstructor.this);
			builder.setTitle(R.string.dialogTitle_missingInstructorName);	//set message title from string resource
			builder.setMessage(R.string.dialogMsg_missingInstructorName);//set message content
			builder.setPositiveButton("OK", null);		//ok button to dismiss the dialog box

			AlertDialog errorDialog = builder.create();
			errorDialog.show();
		}
		//return true;
		return super.onOptionsItemSelected(item);
	}
}

